package com.example.myapplication;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

public class ratelist extends ListActivity {

    private static final String TAG = "exchange";
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        handler = new Handler(Looper.getMainLooper()){
            public void handleMessage(Message msg){
                if(msg.what==7){
                    List<String> list2 = (List<String>) msg.obj;
                    ListAdapter adapter = new ArrayAdapter<String>(ratelist.this,
                            android.R.layout.simple_expandable_list_item_1,list2);

                    setListAdapter(adapter);
                }
                super.handleMessage(msg);
            }
        };



        String[] list_data = {"熊大","熊二","光头强","肥波"};
        ListAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1,list_data);
        setListAdapter(adapter);

        Thread t = new Thread(()->{
            
            List<String> list1 = new ArrayList<String>();
            try{
                Thread.sleep(2000);
                try{
                    Document doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/").get();
                    Log.i(TAG,"run:title=" + doc.title());

                    Elements tables = doc.getElementsByTag("table");
                    Element table = (Element) tables.get(1);
                    Elements trs = table.getElementsByTag("tr");
                    trs.remove(0);
                    for (Element tr : trs){
                        Elements tds = tr.children();
                        Element td1 = tds.first();
                        Element td2 = tds.get(5);
                        String str1 = td1.text();
                        String str2 = td2.text();
                        list1.add(str1+"==>"+str2);

                        Log.i(TAG,"run"+ str1 + "==>" + str2);
                    }
                }catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }catch (InterruptedException e){
                throw new RuntimeException(e);
            }

            

            Message msg =handler.obtainMessage(7,list1);
            handler.sendMessage(msg);
        });
        t.start();
    }
}